package make.free.app.mcaro.game;

import make.free.app.mcaro.Dialog.Dialog_Chat;
import make.free.app.mcaro.activity.Game;
import make.free.app.mcaro.bluetooth.BluetoothChatService;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.content.Context;

public class Chat {
	private BitmapTextureAtlas prb_BTAonline;
	private static Sprite prb_SPonline;
	private TextureRegion prb_TRonline;
	private BitmapTextureAtlas prb_BTAoffline;
	private static Sprite prb_SPoffline;
	private static TextureRegion prb_TRoffline;
	private BluetoothChatService mChatService;
	private Context mContext;
	private Engine mEngine;
	private Scene mScene;
	private  static int position;

	public Chat(Context mContext, Engine mEngine, Scene mScene, int position,BluetoothChatService mChatService) {
		super();
		this.mChatService =mChatService;
		this.mContext = mContext;
		this.mEngine = mEngine;
		this.mScene = mScene;
		Chat.position = position;
	}

	@SuppressWarnings("static-access")
	public void onLoadResources() {
		this.prb_BTAonline = new BitmapTextureAtlas(128, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.prb_TRonline = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.prb_BTAonline, mContext,
						"chat_online.png", 0, 0);
		this.mEngine.getTextureManager().loadTextures(this.prb_BTAonline);

		this.prb_BTAoffline = new BitmapTextureAtlas(128, 128,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.prb_TRoffline = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.prb_BTAoffline, mContext,
						"chat_offline.png", 0, 0);
		this.mEngine.getTextureManager().loadTextures(this.prb_BTAoffline);

	}
	@SuppressWarnings("static-access")
	public void onLoadScene() {

		this.prb_SPoffline = new Sprite(position-prb_TRoffline.getWidth()-5,2,this.prb_TRoffline)
		{
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
					prb_SPonline.setPosition(-800, -800);
					prb_SPoffline.setPosition(position-prb_TRoffline.getWidth()-5, 2);
					Dialog_Chat dialog = new Dialog_Chat(mContext,mChatService);
					dialog.show();
				}
				return true;
			}
		};
		mScene.registerTouchArea(prb_SPoffline);
		this.mScene.attachChild(prb_SPoffline);
		
		//icon online
		this.prb_SPonline = new Sprite(-100,-100,this.prb_TRonline)
		{
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_DOWN) {
					Game.isChat =false;
					prb_SPonline.setPosition(-800, -800);
					prb_SPoffline.setPosition(position-prb_TRoffline.getWidth()-5, 2);
					Dialog_Chat dialog = new Dialog_Chat(mContext,mChatService);
					dialog.show();
				}
				return true;
			}
		};
		mScene.registerTouchArea(prb_SPonline);
		this.mScene.attachChild(prb_SPonline);
	}
	/**
	 * change icon when data chat show
	 */
	public void onchageICONOline()
	{
		prb_SPoffline.setPosition(-800, -800);
		prb_SPonline.setPosition(position-prb_TRoffline.getWidth()-5, 2);
	}
	public static void onchageICONOffline()
	{
		prb_SPoffline.setPosition(position-prb_TRoffline.getWidth()-5, 2);
		prb_SPonline.setPosition(-800, -800);
	}
}
